package com.barber.service;


import com.barber.entity.Barber;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.barber.mapper.BarberMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaberServiceImpl implements BarberService {
	Logger logger = Logger.getLogger(BaberServiceImpl.class);

	String key = "";

//	String mysql = "select * from barberinfo";

	@Autowired
	BarberMapper barberMapper;

	@Override
	public boolean insertBaber(Barber barber) {
		logger.debug(barber);

//        boolean b=sqlSessionFactory.openSession().insert("com.eat.mapper.MenuMapper.insert",m)>0;
		boolean b = barberMapper.insert(barber);
		logger.debug(b);
		logger.debug(barber);
		return b;
	}

	@Override
	public boolean deleteBaber(Map map) {

		logger.debug(map);
		boolean b = barberMapper.delete(map);
		logger.debug(b);
		return b;
	}

	@Override
	public boolean updateBaber(Barber barber) {
		logger.debug(barber);
		boolean b = barberMapper.update(barber);
		logger.debug(b);
		return b;
	}

	@Override
	public Map getBarbers(Map params) {
		logger.debug(params);//  {pageIndex=0, sortField=createtime, sortOrder=desc, pageSize=10}为什么获取的是这个
		Map<String, Object> map = new HashMap<>();
		String username = (String) params.get("key");
		int pageIndex = 0;
		int pageSize = 10;

		try {
			pageIndex = Integer.parseInt((String) params.get("pageIndex"));
		} catch (Exception e) {
			// 处理异常或设置默认值
		}

		try {
			pageSize = Integer.parseInt((String) params.get("pagesize"));
		} catch (Exception e) {
			// 处理异常或设置默认值
		}

		// 计算分页的偏移量
		int offset = pageIndex * pageSize;

		// 查询数据列表
		List<Barber> barberList = barberMapper.selectBarbersByName(username, offset, pageSize);
		logger.debug(barberList);
		// 查询总数
		int totalCount = barberMapper.selectBarberCountByName(username);
logger.debug(totalCount);
		// 将结果放入HashMap中
		map.put("total", totalCount);
		map.put("data", barberList);

		return map;
	}

	@Cacheable(value = "barbersCache")
	@Override
	public List<Barber> selectAllBarber() {
		logger.debug("test cache");//没进入说明从缓存取到了
		return barberMapper.selectAllBarber();

	}

	@Override
	public Map login(Map map) {
		Map result= (Map) barberMapper.getOne(map);
		logger.debug(result);
		return result;
	}
	@Override
	public Map getinfo(Map map) {
		logger.debug(map);

//        Map result= sqlSessionFactory.openSession().selectOne("com.lx.mapper.SellerMapper.select",map);
		Map b= barberMapper.getOne(map);
		logger.debug(b);
		return b;
	}

}
